//
//  WorkerChatSettingViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/31.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "WorkerChatSettingViewController.h"

@interface WorkerChatSettingViewController ()
{
    UISwitch *switchBtn;
    CustomView *actionSheet;
}
@end

@implementation WorkerChatSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"聊天设置";
    
    [self initUI];
}
- (void)initUI {
    self.view.backgroundColor = THEMECOLOR_OF_GRAY;
    
    UIView *workerBgView = [UIToolsControl initViewWithFrame:CGRectMake(0, 10*Scale, WIDTH, 70*Scale) backgroundColor:COLOR_OF_WHITE];
    [self.view addSubview:workerBgView];
    UIImageView *imgView = [UIToolsControl initImageViewWithFrame:CGRectMake(LeftSpace, 15*Scale, 40*Scale, 40*Scale) imageName:@"header_bg"];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.layer.cornerRadius = 20*Scale;
    imgView.layer.masksToBounds = YES;
    [workerBgView addSubview:imgView];
    if (self.user_header&&self.user_header.length) {
        [imgView sd_setImageWithURL:[NSURL URLWithString:self.user_header]];
    }
    UILabel *nameLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(imgView)+20*Scale, 0, 200, ViewHeight(workerBgView)) text:self.user_name?self.user_name:@"" textColor:THEMECOLOR_OF_TEXT_666666 font:Font_14];
    [workerBgView addSubview:nameLbl];
    
    UIView *rejectBgView = [UIToolsControl initViewWithFrame:CGRectMake(0, EndY(workerBgView)+5*Scale, WIDTH, 45*Scale) backgroundColor:COLOR_OF_WHITE];
    [self.view addSubview:rejectBgView];
    UILabel *rejectLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, 0, 300, ViewHeight(rejectBgView)) text:@"不接受对方的消息" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_18];
    [rejectBgView addSubview:rejectLbl];
    switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTH-75*Scale, 3*Scale, 45*Scale, 22*Scale)];
    switchBtn.thumbTintColor = COLOR_OF_WHITE;
    switchBtn.onTintColor = THEMECOLOR_OF_ORANGE;
    [switchBtn addTarget:self action:@selector(switchBtnTap:) forControlEvents:UIControlEventValueChanged];
    switchBtn.transform = CGAffineTransformMakeScale(0.75, 0.75);
    [rejectBgView addSubview:switchBtn];
    switchBtn.on = ([self.inBlacklist isEqualToString:@"1"])?YES:NO;
    
    UIButton *clearnBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, EndY(rejectBgView)+5*Scale, WIDTH, ViewHeight(rejectBgView)) imageName:nil target:self action:@selector(deleteBtnTap) title:@"清空聊天记录" titleColor:THEMECOLOR_OF_TEXT_333333];
    clearnBtn.titleLabel.font = Font(Font_18);
    clearnBtn.backgroundColor = COLOR_OF_WHITE;
    [self.view addSubview:clearnBtn];
    
    UIButton *reportBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, EndY(clearnBtn)+5*Scale, WIDTH, ViewHeight(rejectBgView)) imageName:nil target:self action:@selector(reportBtnTap) title:@"举报" titleColor:THEMECOLOR_OF_RED];
    reportBtn.backgroundColor = COLOR_OF_WHITE;
    reportBtn.titleLabel.font = Font(Font_18);
    [self.view addSubview:reportBtn];
    
    UIButton *deleteBtn = [UIToolsControl initButtonWithFrame:CGRectMake(40*Scale, EndY(reportBtn)+110*Scale, WIDTH-80*Scale, 46*Scale) imageName:nil target:self action:@selector(deleteBtnTap) title:@"删除对话" titleColor:COLOR_OF_WHITE];
    deleteBtn.layer.cornerRadius = 23*Scale;
    deleteBtn.layer.masksToBounds = YES;
    deleteBtn.backgroundColor = THEMECOLOR_OF_RED;
    deleteBtn.titleLabel.font = Font(Font_18);
    [self.view addSubview:deleteBtn];
    deleteBtn.hidden = YES;
    if (self.id&& self.id.length) {
        deleteBtn.hidden = NO;
    }
}
- (void)deleteBtnTap {
    NSLog(@"删除");
    [[TIMManager sharedInstance] deleteConversation:TIM_C2C receiver:self.id];
    SVPHUDSuccess(@"操作成功");
    [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.5];
}
- (void)delayClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)switchBtnTap:(UISwitch *)switchButton {
    NSLog(@"switchBtn:%@", switchButton.on?@"YES":@"NO");
    
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:self.id forKey:@"target_uid"];
    [dic setObject:(switchButton.on?@"1":@"0") forKey:@"status"];
    
    [BaseNetManager requestWithMethod:POST path:Company_IMAddBlacklist_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)reportBtnTap {
    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:0];
    if ([DataToolsControl sharedManager].allCategoryInfoModel && [DataToolsControl sharedManager].allCategoryInfoModel.reportArray.count) {
        for (PublicCategoryElementModel *model in [DataToolsControl sharedManager].allCategoryInfoModel.reportArray) {
            [dataArray addObject:model.name];
        }
    }
    __weak __typeof(self)weakSelf = self;
    actionSheet = [[CustomView alloc] initWithTitle:nil cancelButtonTitle:@"取消" otherButtonTitles:dataArray actionSheetBlock:^(NSInteger buttonIndex) {
        NSLog(@"ACActionSheet nnnn block - %ld", buttonIndex);
        [weakSelf reportData:buttonIndex];
    }];
    [actionSheet showSheetView];
}
- (void)reportData:(NSInteger)index {
    PublicCategoryElementModel *model = [DataToolsControl sharedManager].allCategoryInfoModel.reportArray[index];
    
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:model.id forKey:@"typeid"];
    [dic setObject:UD_GetOForK(isUserID) forKey:@"userid"];
    
    [BaseNetManager requestWithMethod:POST path:Chat_Report_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

@end
